package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;

    @Autowired
    private CarPackageAuditServiceImpl carPackageAuditService;

    @Autowired
    private BpmnInfoServiceImpl bpmnInfoService;

    @Autowired
    private RuntimeService runtimeService;

    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        //判断是否是套餐
        if(serviceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())){
            //如果是套餐,需要把审核状态设置初始化
            serviceItem.setAuditStatus(serviceItem.AUDITSTATUS_INIT);
        }else{
                //如果不是套餐,需要把审核状态设置无需审核/
                serviceItem.setAuditStatus(serviceItem.AUDITSTATUS_NO_REQUIRED);
            }
        serviceItemMapper.insert(serviceItem);
        }


    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem item = this.get(serviceItem.getId());
        //如果是上架状态，不允许修改的,抛出异常
        if(serviceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus())){
            throw new BusinessException("上架服务项目不能修改，请下架后再修改");
        }else if(serviceItem.AUDITSTATUS_AUDITING.equals(serviceItem.getAuditStatus())){
            throw new BusinessException("服务项目审核中不能修改");
        }
        //下架状态
        //审核通过 则设置为初始化
        if(ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    //下
    public void saleOff(Long id) {
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_OFF);
    }

    @Override
    //上
    public void saleOn(Long id) {
        ServiceItem serviceItem = get(id);
        //是套餐，审核没通过出异常
        if(ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage()) && !ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())){
            throw new BusinessException("套餐服务项需要审核通过之后才能上架");
        }
        //不是套餐，直接上架
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_ON);
    }

    @Override
    public void startProcess(Long id, Long bpmnInfoId, Long director, Long finance, String info) {
        //合理化校验
        //根据传入的id查询服务项,服务项状态出于初始化才能进行审核逻辑
        ServiceItem serviceItem = this.get(id);
        //当serviceItem等于空或状态不等于初始化则抛出异常
        if(serviceItem == null || serviceItem.getAuditStatus() != ServiceItem.AUDITSTATUS_INIT){
            throw new BusinessException("参数异常");
        }
        //修改服务状态,把初始化改为审核中
        serviceItemMapper.changeAuditStatus(id,ServiceItem.AUDITSTATUS_AUDITING);
        //审核信息记录
        CarPackageAudit audit = new CarPackageAudit();
        audit.setServiceItemId(id);
        //设置备注信息
        audit.setServiceItemInfo(serviceItem.getInfo());
        //设置折扣价
        audit.setServiceItemPrice(serviceItem.getDiscountPrice());
        //设置当前时间
        audit.setCreateTime(new Date());
        //设置创建人
        audit.setCreator(ShiroUtils.getUser().getUserName());
        //设置套餐名称
        audit.setServiceItemName(serviceItem.getName());
        //设置bpmn信息的id
        audit.setBpmnInfoId(bpmnInfoId);
        audit.setInfo(info);
        //设置审核人的id
        audit.setAuditorId(director);
        //保存
        carPackageAuditService.save(audit);
        //启动流程实例
        Map<String, Object> map = new HashMap<>();
        map.put("director",director.toString());
        if(finance != null){
            map.put("finance",finance.toString());
        }
        map.put("discountPrice",serviceItem.getDiscountPrice().longValue());

        BpmnInfo bpmnInfo = bpmnInfoService.get(bpmnInfoId);
        //启动流程
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey(bpmnInfo.getActProcessKey(),
                        audit.getId().toString()
                        ,map);
        //更新记录实例id
        audit.setInstanceId(processInstance.getId());
        carPackageAuditService.updateInstanceId(audit);
    }

    @Override
    public void changeAuditStatus(Long serviceItemId, Integer auditstatusInit) {
        serviceItemMapper.changeAuditStatu(serviceItemId,auditstatusInit);
    }

    @Override
    public void updateAuditStatus(Long serviceItemId, Integer auditstatusInit) {
        serviceItemMapper.updateAuditStatus(serviceItemId,auditstatusInit);
    }
}
